package com.wujian.love.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import com.wujian.love.domin.Information;
import com.wujian.love.repository.InformationRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.query.SortQueryBuilder;
import org.springframework.stereotype.Service;
import com.wujian.love.mapper.InformationMapper;
import com.wujian.love.service.IInformationService;

/**
 *  服务层实现
 * 
 * @author cdw
 * @date 2020-12-22
 */
@Service
public class InformationServiceImpl implements IInformationService
{
	@Autowired
	private InformationMapper informationMapper;


	@Autowired
	private InformationRepository informationRepository;

	/**
     * 查询信息
     * 
     * @param id ID
     * @return 信息
     */
    @Override
	public Information selectInformationById(Long id)
	{
	    return informationMapper.selectInformationById(id);
	}
	
	/**
     * 查询列表
     * 
     * @param information 信息
     * @return 集合
     */
	@Override
	public List<Information> selectInformationList(Information information)
	{
	    return informationMapper.selectInformationList(information);
	}
	
    /**
     * 新增
     * 
     * @param information 信息
     * @return 结果
     */
	@Override
	public int insertInformation(Information information) {
		int row = informationMapper.insertInformation(information);
		if(row > 0){
			informationRepository.save(information);
		}
	    return row;

	}
	
	/**
     * 修改
     * 
     * @param information 信息
     * @return 结果
     */
	@Override
	public int updateInformation(Information information)
	{
	    return informationMapper.updateInformation(information);
	}

	/**
	 * @Author wu
	 * @Description //查询所有的信息
	 * @Date 15:42 2020/12/24
	 * @Param []
	 * @return java.util.List<com.wujian.love.domin.Information>
	**/
	@Override
	public List<Information> getAllInformation() {
		List<Information> list = Lists.newArrayList(informationRepository.findAll(Sort.by(Sort.Direction.DESC, "id")));
		return list;
	}

	/**
	 * @Author wu
	 * @Description //查询
	 * @Date 16:52 2020/12/24
	 * @Param [type, keyWord]
	 * @return java.util.List<com.wujian.love.domin.Information>
	**/
	@Override
	public List<Information> getInformationByType(String type, String keyWord) {
		List<Information> list = new ArrayList<>();
		if(type.equals("mysql")){
			list = informationMapper.getInformationByType(keyWord);
		}else{
			String vague = "*"+keyWord+"*";
			//模糊查询条件
			BoolQueryBuilder builder = QueryBuilders.boolQuery();
			builder.should(QueryBuilders.wildcardQuery("title",vague));
			builder.should(QueryBuilders.wildcardQuery("content",vague));
			builder.should(QueryBuilders.wildcardQuery("author",vague));
			//排序
			FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort("id").order(SortOrder.DESC);
			//构建查询语句
			NativeSearchQuery build = new NativeSearchQueryBuilder()
					.withQuery(builder)
					.withSort(fieldSortBuilder)
					.build();
			Page<Information> search = informationRepository.search(build);
			list = search.getContent();
		}
		return list;
	}


}
